package scala.maven;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Produces Scala API documentation
 *
 * @goal doc
 * @requiresDependencyResolution compile
 * @phase process-sources
 *
 * TODO find a way to integrate this into Maven's broader site/reporting structure
 */
public class ScalaDocMojo extends ScalaMojo {

    /**
     * @parameter expression="${project.build.directory}/apidocs"
     */
    protected File apiDocPath;

    /**
     * @parameter default-value="src/main/scala"
     */
    protected File sourceDir;

    
    
    public void execute() throws MojoExecutionException, MojoFailureException {
        super.execute();

        if (!apiDocPath.exists())
             apiDocPath.mkdirs();

        List<String> sourceFiles;
        try {
            sourceFiles = findFiles(sourceDir, File.separator, ".*\\.scala");
        } catch (Exception e) {
            throw new MojoExecutionException("Didn't find any scala source files in " + sourceDir.getAbsolutePath());
        }

        List<String> command = new ArrayList<String>();
        command.add(javaBin);
        //see ScalaMojo
        super.addMemoryOptions(command);
        command.add("-classpath");
                command.add("-classpath");
        command.add(getClassPath(classpathElements));
        command.add("scala.tools.nsc.Main");
        command.add("-doc");
        command.add("-d");
        command.add(apiDocPath.getAbsolutePath());
        command.add("-sourcepath");
        command.add(sourceDir.getAbsolutePath());
        for (String x : sourceFiles) {
            command.add(sourceDir + File.separator + x);
        }
        runCommand(command.toArray(new String[0]));
    }
}
